<?php
namespace app\audit_api\controller;
use app\audit_api\service\Accept as AcceptService;
use app\constdir\ApiConst;
use app\constdir\AuditApiConst;
use think\Request;

/**
 * Class Accept
 * @package app\audit_api\controller
 * @description 处理钉钉返回的回调信息
 */
class Accept extends Base
{
    protected $accept_service;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        $this->accept_service = new AcceptService();
    }

    /**
     * @introduction:回调注册接口以及内容处理
     * @return mixed
     * @author:cxh
     * @date: 2020/6/8 17:27
     */
    public function index(){


        $signature = isset($_GET['signature']) ? $_GET['signature']:'';
        $timeStamp = isset($_GET['timestamp']) ? $_GET['timestamp']:'';
        $nonce = isset($_GET['nonce']) ? $_GET['nonce']:'';

        $postdata = file_get_contents("php://input");
        $postList = json_decode($postdata, true);
        $encrypt = $postList['encrypt'];

        //回调解密
        $DecryptCode = $this->accept_service->DecryptMsg($signature, $timeStamp, $nonce, $encrypt);

        if(!empty($DecryptCode['msg'])){

            $info = json_decode($DecryptCode['msg'] , true);

            if($info['type'] == 'finish' and $info['result'] == 'agree'){
                $data = [
                    'EventType' => $info['EventType'],
                    'processInstanceId' => $info['processInstanceId'],
                    'corpId' => $info['corpId'],
                    'createTime' => $info['createTime'],
                    'finishTime' => $info['finishTime'],
                    'title' => $info['title'],
                    'type' => $info['type'],
                    'staffId' => $info['staffId'],
                    'url' => $info['url'],
                    'result' => $info['result'],
                    'processCode' => $info['processCode'],
                    'businessId' => $info['businessId'],
                    'addtime' => date("Y-m-d H:i:s")
                ];

                //将审批成功数据入库
                @$this->accept_service->callBackInfoSave($data);
            }

            //记录日志数据
            file_put_contents('../runtime/log/dingding.log',date("Y-m-d H:i:s").' 接收到审批:'.$DecryptCode['msg'].PHP_EOL,FILE_APPEND);

        }


        //处理成功返回
        $res = "success";
        //回调加密
        $EncryptCode = $this->accept_service->EncryptMsg($res, $timeStamp, $nonce);

        return $EncryptCode['msg'];
    }


    /**
     * @introduction:注册业务事件回调接口
     * @return array|mixed
     * @author:cxh
     * @date: 2020/6/8 19:26
     */
    public function registerCallBack(){
        $data = $this->accept_service->registerCallBack();
        if($data['code'] != 0 or $data['msg'] != 'ok'){
            $data = ['code'=> $data['code'], 'msg'=> $data['msg']];
        }
        return $this->apiSuccessError($data);
    }

    /**
     * @introduction:更新事件回调接口
     * @return array|mixed
     * @author:cxh
     * @date: 2020/6/8 19:27
     */
    public function updateCallBack(){

        $data = $this->accept_service->updateCallBack();
        if($data['code'] != 0 or $data['msg'] != 'ok'){
            $data = ['code'=> $data['code'], 'msg'=> $data['msg']];
        }
        return $this->apiSuccessError($data);

    }


    /**
     * @introduction:删除企业回调
     * @return array|mixed
     * @author:cxh
     * @date: 2020/6/8 19:27
     */
    public function deleteCallBack(){

        $data = $this->accept_service->deleteCallBack();
        if($data['code'] != 0 or $data['msg'] != 'ok'){
            $data = ['code'=> $data['code'], 'msg'=> $data['msg']];
        }
        return $this->apiSuccessError($data);

    }

}